﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Honeycomb
{
    class Bee
    {
        public const double HoneyUnitsConsumedPerMg = 0.25;

        public double WeightMg { get; private set; }

        public Bee(double weightMg)
        {
            WeightMg = weightMg;
        }

        public virtual double HoneyConsumptionRate()
        {
            return WeightMg * HoneyUnitsConsumedPerMg;
        }
    }

    class Worker:Bee
    {
        private string[] _jobsICanDo;

        private int _shiftsToWork;

        private int _shiftsWorked;

        private string _currentJob = "";

        public Worker(string[] jobsICanDo, double weightMg) : base(weightMg)
        {
            _jobsICanDo = jobsICanDo;
        }

        public int ShiftsLeft => _shiftsToWork - _shiftsWorked;

        public string CurrentJob => _currentJob;

        public bool DoThisJob(string job, int numberOfShifts)
        {
            if (!string.IsNullOrEmpty(_currentJob))
            {
                return false;
            }

            foreach (var _job in _jobsICanDo)
            {
                if (_job == job)
                {
                    _currentJob = job;
                    _shiftsToWork = numberOfShifts;
                    _shiftsWorked = 0;
                    return true;
                }
            }

            return false;
        }

        public bool DidYouFinish()
        {
            if (string.IsNullOrEmpty(_currentJob))
            {
                return false;
            }

            _shiftsWorked++;

            if (_shiftsWorked > _shiftsToWork)
            {
                _shiftsWorked = 0;
                _shiftsToWork = 0;
                _currentJob = "";
                return true;
            }
            return false;
        }

        private const double HoneyUnitsPerShiftWorked = 0.65;

        public override double HoneyConsumptionRate()
        {
            return base.HoneyConsumptionRate() + _shiftsWorked * HoneyUnitsPerShiftWorked;
        }
    }

    class Queen:Bee
    {
        private Worker[] _workers;

        private int _shiftNumber;

        public Queen(Worker[] workers,double weightMg):base(weightMg)
        {
            _workers = workers;
        }

        public bool AssignWork(string job, int numberOfShifts)
        {
            foreach (var worker in _workers)
            {
                if (worker.DoThisJob(job, numberOfShifts))
                {
                    return true;
                }
            }

            return false;
        }

        public string WorkTheNextShift()
        {
            double honeyConsumed = HoneyConsumptionRate();

            _shiftNumber++;
            string report = "Report for shift #" + _shiftNumber + "\r\n";
            for (var i = 0; i < _workers.Length; i++)
            {
                honeyConsumed += _workers[i].HoneyConsumptionRate();

                if (_workers[i].DidYouFinish())
                {
                    report += "Worker #" + (i + 1) + " finished the job\r\n";
                }
                if (string.IsNullOrEmpty(_workers[i].CurrentJob))
                {
                    report += "Worker #" + (i + 1) + " is not working\r\n";
                }
                else
                {
                    if (_workers[i].ShiftsLeft > 0)
                    {
                        report += "Worker #" + (i + 1) + " is doing '" + _workers[i].CurrentJob + "' for " +
                                  _workers[i].ShiftsLeft + " more shifts\r\n";
                    }
                    else
                    {
                        report += "Worker #" + (i + 1) + " will be done with '" + _workers[i].CurrentJob +
                                  "' after this shift\r\n";
                    }
                }
            }
            report += "Total honey consumed for the shift: " + honeyConsumed + " units\r\n";
            return report;
        }
    }
}
